<template>
  <div class="page-content">
    <div class="header" style="margin-bottom: 1.5rem">
      <h3>操作日志</h3>
    </div>
    <el-row style="margin-top: 1.5rem; width: 100%">
      <div style="width: 100%">
        <el-form :model="searchForm" ref="searchFormRef" label-width="auto">
          <el-row :gutter="24">
            <el-col :span="6">
              <el-form-item label="账号搜索" prop="name">
                <el-input placeholder="请输入账号" v-model="searchForm.name" clearable @blur="getData"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="操作时间：">
                <el-date-picker label="操作时间：" v-model="datavalue1" type="datetimerange" start-placeholder="开始日期"
                  end-placeholder="结束日期" format="YYYY-MM-DD HH:mm:ss" date-format="YYYY/MM/DD ddd"
                  time-format="A hh:mm:ss" @change="createTime" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-row>

    <art-table :data="tableData" :loading="tableLoading" :currentPage="pageNum" :pageSize="pageSize" :total="pageTotal"
      @current-change="currentChange" @size-change="sizeChange">
      <template #default>
        <el-table-column label="ID" prop="id" width="100" />
        <el-table-column label="操作账号" prop="operator" align="center" />
        <el-table-column label="操作时间：" prop="createTime" align="center">
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="IP地址：" prop="ip" align="center">
          <template #default="scope">
            {{ scope.row.ip }}
          </template>
        </el-table-column>
        <el-table-column label="操作类型" prop="method" align="center">
          <template #default="scope">
            {{ scope.row.method }}
          </template>
        </el-table-column>
        <el-table-column label="操作内容" prop="content" align="center">
          <template #default="scope">
            {{ scope.row.content }}
          </template>
        </el-table-column>
      </template>
    </art-table>
  </div>
</template>

<script setup lang="ts">
import { useMenuStore } from '@/store/modules/menu'
import type { FormInstance, FormRules } from 'element-plus'
import { formatMenuTitle } from '@/router/utils/utils'
import { loginLog } from '@/api/driverSchoolApi'

const permissionDialog = ref(false)
const menuList = computed(() => useMenuStore().getMenuList)

const tableData = ref([])

const defaultProps = {
  children: 'children',
  label: (data: any) => formatMenuTitle(data.meta?.title) || ''
}

const formatDate = (date: string) => {
  return new Date(date)
    .toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    })
    .replace(/\//g, '-')
}
//搜索刷新等
const search = () => {
  getData()
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  datavalue1.value = ''
  searchForm.startTime = ''
  searchForm.endTime = ''
}
const searchFormRef = ref<FormInstance>()
const columns = reactive([
  { name: '用户信息', show: true },
  { name: '手机号', show: true },
  { name: '性别', show: true },
  { name: '来源', show: true },
  { name: '状态', show: true },
  { name: '创建日期', show: true }
])
const changeColumn = (list: any) => {
  columns.values = list
}
const searchForm = reactive({
  name: '',
  phone: '',
  id: '',
  account: '',
  sex: '',
  level: '',
  cardtype: '',
  conttype_one: '',
  conttype_two: '',
  startTime: '',
  endTime: ''
})

const filterEmptyValues = (obj: any) => {
  const result: any = {}
  for (const key in obj) {
    if (obj[key] !== '' && obj[key] !== null && obj[key] !== undefined) {
      result[key] = obj[key]
    }
  }
  return result
}

const datavalue1 = ref('')
const pageNum = ref(1)
const pageSize = ref(10)
const pageTotal = ref(0)
const tableLoading = ref(false)
async function getData() {
  try {
    tableLoading.value = true

    const { code, data } = await loginLog(filterEmptyValues({
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      keywords: searchForm.name,
      queryStartTime: searchForm.startTime ? formatDate(searchForm.startTime) : '',
      queryEndTime: searchForm.endTime ? formatDate(searchForm.endTime) : '',
      queryCondition: 'OPERATION'
    }))
    if (code === '00000') {
      tableData.value = data.list
      pageTotal.value = data.total
      console.log(tableData.value, 'tableData.value')
    }
  } catch (e) {
  } finally {
    tableLoading.value = false
  }
}
const currentChange = (val: number) => {
  pageNum.value = val
  getData()
}

const sizeChange = (val: number) => {
  pageSize.value = val
  pageNum.value = 1
  getData()
}

const createTime = (e: any) => {
  if (e) {
    searchForm.startTime = e[0]
    searchForm.endTime = e[1]
  } else {
    searchForm.startTime = ''
    searchForm.endTime = ''
  }
  getData()
}

onMounted(() => {
  getData()
})
</script>

<style lang="scss" scoped>
.page-content {
  .svg-icon {
    width: 1.8em;
    height: 1.8em;
    overflow: hidden;
    vertical-align: -8px;
    fill: currentcolor;
  }
}

.tag-item+.tag-item {
  margin-left: 5px;
}
</style>
